﻿using System;
using System.Collections.Generic;
using AudioStudio;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.Survivor
{
    /// <summary>
    /// 技能目标
    /// </summary>
    public class SkillTarget : IClass
    {
        public BattleUnit source;
        public BattleUnit unit;

        public Vector3 position;

        public Vector3 forward;
        
        public ESkillTargetType targetType;
        
        public int advancedFrame;
        public float searchTargetTime;
        public int bulletStartFrame;
        public bool isValid = false;

        public BulletConfig bulletConfig;
        public void OnGetTarget()
        {
            if (bulletConfig.locktarget_show_collision > 0)
            {
                var position = GetTargetPosition();
                if (bulletConfig.shape_type == (int)EBulletShapeType.Rect ||
                    bulletConfig.shape_type == (int)EBulletShapeType.Sector)
                {
                    position = source.Position;
                }
                //显示指示器
                BattleEffectManager.Instance.ShowSkillDirector(
                    position,
                    GetTargetForward(),
                    bulletConfig.locktarget_show_collision,
                    bulletConfig.shape_type,
                    bulletConfig.shape_params.ToArray()
                );
            }
        }

        public Vector3 GetTargetPosition()
        {
            if (unit != null)
            {
                return unit.Position;
            }
            return position;
        }
        
        public Vector3 GetTargetForward()
        {
            return GetTargetPosition() - source.Position;
        }

        public void Clear()
        {
            unit = null;
            position = Vector3.zero;
            forward = Vector3.zero;
            isValid = false;
        }
        
        public void OnReset()
        {
            Clear();
            bulletConfig = null;
            source = null;
        }
    }
}
